#ifndef MyDataCenter_H
#define MyDataCenter_H

#include "data.h"
#include "http\clientengine.h"
#include "http\DownloadEngine.h"
#include "mk4.h"
#include "mk4str.h"
#include "MessageCaller.h"
#include <html\htmlctlevent.h>
#include <html\lirenderer.h>

#include "hm\hm_tab.h"
#include "hm\hm_preview.h"
#include "hm\hm_popmenu.h"
#include "hm\hm_util.h"
#include "hm\hm_gridview.h"
#include "ui\mywaitdlg.h"
#include "ui\MyTimer.h"
#include "myutils.h"
#include "plugin.h"

class CBtEngine;
class CBluetoothPMPExampleEngine;
class CImageHandler;
class MyRichEditor;
class CMyDataCenter;
class MyDialog;
class MyWaitDlg;

extern CMyDataCenter* iDataCenter;

//const
//_LIT(KHomeURL, "c:\\data\\testedit\\index.xml");
_LIT(KHomeURL, "c:\\data\\testedit\\login.xml");

_LIT8(KTestIP, "http://127.0.0.1:8081/");
#ifdef _DEBUG
//	_LIT8(KRemoteIP, "http://127.0.0.1:8081/");
//	_LIT8(KRemoteMail, "http://127.0.0.1:8081/");	
	_LIT8(KRemoteIP, "http://221.130.184.102:8081/");
	_LIT8(KRemoteMail, "http://221.130.184.102:8081/");	
	_LIT8(KRemoteImg, "http://221.130.184.102/moimages/%04d/%06d/%06d_%02d_m.jpg");
	_LIT8(KRemotePreImg, "http://221.130.184.102/moimages/%04d/%06d/%06d_%02d_s.jpg");	
//	_LIT8(KRemoteImg, "http://127.0.0.1/%03d/%06d_%02d_m.jpg");
//	_LIT(KLocalImg, "c:\\data\\itest\\%04d\\%06d_%02d_m.jpg");	
	_LIT(KLocalImg, "%06d_%02d_m.jpg");	
//	_LIT(KLocalPreviewImg, "c:\\data\\itest\\%04d\\%06d_%02d_s.jpg");		
	_LIT(KLocalPreviewImg, "%06d_%02d_s.jpg");
#else
	_LIT8(KRemoteIP, "http://221.130.184.102:8081/");
//	_LIT8(KRemoteIP, "http://219.233.123.26:8080/");//my own public ip in home
	_LIT8(KRemoteMail, "http://221.130.184.102:8081/");
//	_LIT8(KRemoteMail, "http://219.233.123.26:8081/");//my own public ip in home
	_LIT8(KRemoteImg, "http://221.130.184.102/moimages/%04d/%06d/%06d_%02d_m.jpg");
	_LIT8(KRemotePreImg, "http://221.130.184.102/moimages/%04d/%06d/%06d_%02d_s.jpg");	
//	_LIT(KLocalImg, "e:\\data\\itest\\%04d\\%06d_%02d_m.jpg");		
	_LIT(KLocalImg, "%06d_%02d_m.jpg");		
//	_LIT(KLocalPreviewImg, "e:\\data\\itest\\%04d\\%06d_%02d_s.jpg");
	_LIT(KLocalPreviewImg, "%06d_%02d_s.jpg");
#endif

typedef TBuf<128> BUF128;
/*
 * my url info, always included as items as, <1>url, <2>func, <3>... func paras...
 */
typedef RArray<BUF128> URLInfo;

//current pageinfo
typedef struct PAGEDATAINFO
{
	int categoryid;
	int siteid;
	int channelid;
	int pageid;
	int imgindex;
	int imagenum;//only images that downloaded from http
};

/*
 * 1-下载页面里的图片，比如图片论坛类
 * 2-只为页面内的某个image元素下载一副图，而且指定目标文件名
 */
typedef struct IMGDOWNTASK
{
	int method;//1 or 2
	
	//1-for page images	
	int channelid;
	int pageid;
	int imgindex;
	int totalnum;
	
	//2 for destinated image in page，
	TBuf<100> dest;
};

class c4_Storage;
class c4_View;
class MSyDocHandler;
class CFeedbackHandler;
class MyPopMenu;
class MCoeView;

class CMyDataCenter : public CBase, public MClientObserver,  
	public MHtmlCtlEventObserver, MListItemRenderer, MTimerCallback,
	MPluginObserver
{
friend class CiTestAppUi;
friend class CMyPreview;
friend class CITestImageContainer;
public: // Constructors and destructor
	CMyDataCenter();
	~CMyDataCenter();
	
public:
	void ClientEvent(const int aClientID, const TDesC& aEventDescription);
	void ClientReceived(const int aClientID, const TDesC8& aRevData);	
	void ClientBodyReceived(const int aClientID, const TDesC8& aBodyData);		

	void UnloadPlugin(MMyDll* aPlugin);
public:
	CiTestAppUi* AppUi();
	void ClearImageList();		
	void CancelLoading();
	void LoadHomePage();

	//查询大图是否已经存在
	bool FindPageImage(int pageid, int imgindex);	
	void DownloadPageImage(const TDesC& largepageimage);
	//从资源数据库里读出图片的尺寸信息
	bool FastImageInfo(CImageHandler* aImageHandler, const TDesC& aFileName, TSize& size);	
    void HandleBufImageLoadEventL(CBufferedImageDecoder* aDecoder, const TDesC& aFileName);	
    //
   	void HandleHtmlCtlEventL(const THtmlCtlEvent& aEvent);
    void RenderListItemL(CHtmlElement* aElement, TInt aIndex); 

	TKeyResponse SuperHandleKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType );   	
    void OnTimer(CMyTimer* timer, int eventid, int extrapara); 
public:	
	//database
	int FieldIntValue(c4_View* view, char* fieldname, int pos);
	const TPtrC8 FieldStr8Value(c4_View* view, char* fieldname, int pos);
	const TPtrC FieldStrValue(c4_View* view, char* fieldname, int pos);	
	TPtrC FieldUnicodeValue(c4_View* view, char* fieldname, int pos, bool carriageconvert = false);
	TPtrC8 FieldUtf8Value(c4_View* view, char* fieldname, int pos);	
	const unsigned char* FieldBinValue(c4_View* view, char* fieldname, int pos);
	int FindByInt(c4_View* view, char* fieldname, int keyvalue);
	int FindBy2Int(c4_View* view, char* field1, int keyvalue1, char* field2, int keyvalue2);	
	int FindByStr(c4_View* view, char* fieldname, char* keyvalue);
	void SetRowRefIntValue(c4_RowRef rowref, char* fieldname, int value);
	void SetRowRefStrValue(c4_RowRef rowref, char* fieldname, const TDesC& value);	
	void SetRowRefBinValue(c4_RowRef rowref, char* fieldname, const TDesC8& value);
public:	
	void AddHistory(const TDesC& link);
	void AddSelfHistory(const TDesC& link, bool removecurrent);	
	bool GoBack();	
	int Dateline();
	
	void InitBluetooth();
	bool IsMenuVisible();	
	void IssueHTTPGetL(const TDesC8& aUri, MClientObserver* aObserver);
	void IssueHTTPPostL(const TDesC8& aUri, const TDesC8& aContentType, 
		const TDesC8& aBody, MClientObserver* aObserver = NULL);
	bool LoadPageImage(CImageHandler* aImageHandler, const TDesC& aFileName, int aSelectedFrame = 0);	
	void LoadMemPage(char* resname, const TDesC& path, bool addhistory = true);
	void LoadStyles();
	
	//改变应用的tab
	void NextPrevAppTab();
	void HideMenu();
	void AddToStack(CCoeControl* aControl);
	void PopMenu();	
	bool ProcessPageParameters(const TDesC& paras);		
	bool ProcessLink(const TDesC& paras, bool addhistory = true);	
	void TestMe();
	void PlugIn();
	void HandleConnectorDes8ReceivedL(int aHttpClientID, const TDesC8& aData);
	void SetActiveView(MCoeView* aView, CCoeControl* aContainer) {iActiveView = aView;iActiveContainer = aContainer;};
	void SyncrosizeActiveTab();	
	void FillNewsCataOnTab(int cid, int seltab = 0);
	void FillNewsItemOnList(int cid);
	bool ProcessTabChange(bool bNextPrev);		
protected:
	void SyncrosizeActiveTab2();
	void ChangeCurrentHistoryPara(const TDesC& newpara);
	void FakeImageList(int channel, int pageid, int imgamount);
	void LoadImageViewer(int imagestart);
	void LoadExistImages();
	void Login(const TDesC8& PostTemplate);
	void MailCheckList(const TDesC8& PostTemplate);
	void MailContentRead(const TDesC8& PostTemplate, const TDesC& szLocalId);
	void Mail_List(int indicator);
	void Mail_ReadConfig();
	void Mail_ReadMsg(int localid);		
	void MailReply(const TDesC8& PostTemplate);
	void MailSend(const TDesC8& PostTemplate);
	void NewsContentRead(const TDesC8& PostTemplate, int pageid);	
	void NewsCategoryCheck(const TDesC8& PostTemplate, const TDesC& szCid, const TDesC& szUpdateTime);
	void NewsListCheck(const TDesC8& PostTemplate, int channelid);
	void News_List(int channelid, int batchid);
	void News_ReadMsg(int pageid);		
	void ParseRemoteFeedback(const CFeedbackHandler* feedback);
	void ParseXMLBuffer(const TDesC8& aBuffer, MSyDocHandler* aDocHandler);
	void Post(const TDesC& poststr);			
private:
	int FindImageToGet();
	void LoadStockImage();
	void StartBrowser(const TDesC& aUrl);
	void LoadPluginInfo();
public:
	//论坛社区里的图片,不是richeditor里的页面图片
	CDesCArrayFlat* iImageList; //for container to display	
	int iImageStart; //for container to display
	c4_Storage* iDBRes;//存放小的图片,声音,文字等资源	
	c4_Storage* iDBInfo;
	c4_Storage* iDBMail;
	c4_Storage* iDBStock; //显示用的临时表，主要数据都在server端。
	c4_Storage* iDBClub;
	c4_Storage* iDBTemp; //另一个临时表，空文件
	ChmPopMenu iPopMenu;
	MyDialog* iDialog;
	MyWaitDlg iWaitDlg;
private:
	TBuf<32> iUserid;
	TBuf<32> iMAccountid;
	TBuf<256> iUsername;
	IMGDOWNTASK iImgDownTask;
	
	CBtEngine* iEngine;
	CClientEngine* iHttpEngine;
	CClientEngine* iHttpImgDown;
	CDownloadEngine* iHttpDownload;
	TBuf<256> iCurrentPageUrl;
	PAGEDATAINFO iPageInfo;
	int iBtMode;	
	HBufC8* iResponseBuffer;
	int iTabGroupUid;//当前applicaton tab的uid
	int iCurrentCid;//current categoryid or channelid for news/forum
	HBufC* iTempStrBuffer;
	c4_String iStrTemp; 
	int iMenuID; //current popmenu id
	TBuf<50> iMenuStr;
	ChmTab iHmTab;
	ChmUtil iHmUtil;
	ChmPreview iHmPreview;
	ChmGridView iHmGridView;
	bool iHomePageLoaded;//
	
	//for menu event control
	MCoeView* iActiveView;
	CCoeControl* iActiveContainer;
	CDesCArrayFlat* iHistory;	
	int m_pageIntVar;
	int iAntiLock;//下载小图片时，控制用
	bool iImgDownloading;
	bool iHttpPosting;
	CMyTimer* iTimer;
	MyImgFile iMyImgFile;
	RLibrary iLibrary;
};

#endif // MyDataCenter_H
